<template>
  <div>
    <div v-if="!limit">
      <el-upload :action="action"
                 ref='imgList'
                 :data="uploadData"
                 v-loading="loading"
                 :disabled="disabled"
                 :on-error="error"
                 :limit="limit"
                 :file-list="res"
                 list-type="picture-card"
                 :before-upload="before"
                 :on-success="success"
                 :on-preview="showPreview"
                 :on-remove="removeImg">
        <i class="el-icon-plus"></i>
      </el-upload>
    </div>
    <div v-else>
      <el-upload class="avatar-uploader"
                 ref="uploadDom"
                 :action="action"
                 :data="uploadData"
                 :show-file-list="false"
                 :disabled="disabled"
                 :on-success="success"
                 :on-error="error"
                 :before-upload="before">
        <slot>
          <div v-loading="loading">
            <img v-if="showRes[0]&&showRes[0][showKey]"
                 :src="showRes[0][showKey]"
                 class="avatar">
            <i v-else
               class=" el-icon-plus avatar-uploader-icon"></i>
          </div>
          <div class="dis"
               v-if='showRes[0]&&showRes[0][showKey]'>
            <i class="el-icon-zoom-in"
               @click.stop="showPreview(res)"></i>
            <i class="el-icon-delete"
               @click.stop="removeImg(res)"></i>
          </div>
        </slot>
      </el-upload>
    </div>
    <el-dialog append-to-body
               :visible.sync="imgVis">
      <img width="100%"
           :src="previewImg"
           alt="">
    </el-dialog>
  </div>
</template>

<script>
import { api_url } from '@/config/config'

export default {
  name: 'GlUpload',
  props: {
    res: {
      type: String,
      default() {
        return ''
      }
    },
    showKey: {
      type: String,
      default: 'file_path'
    },
    type: {
      type: String,
      default: 'qn'
    },
    disabled: {
      type: Boolean,
      default: false
    },
    data: {
      type: Object,
      default() {
        return {}
      }
    },
    limit: {
      type: Number | String,
      default: ''
    }
  },
  data() {
    return {
      qn: 'opening/upload.qupload',
      df: '',
      imgVis: false,
      mouse: false,
      action: '',
      previewImg: '',
      showRes: [],
      uploadData: { token: 'FFFX123456' },
      loading: false
    }
  },
  watch: {
    data(val) {
      Object.assign(this.uploadData, this.data)
    },
    res(val) {
      //   this.showRes[0].showKey = val
      if (val.length && this.limit == '1') {
        this.showRes = []
        let o = {}
        o[this.showKey] = val
        this.showRes.push(o)
        // console.log(this.showRes)
      } else if(!val.length) {
        this.showRes = []
        this.res = ''
      }else{
          this.showRes = val
      }
      //   if (!val) {
      //     this.$refs.imgList.clearFiles()
      //     this.res = ''
      //     this.showRes = []
      //   }
    }
  },
  created() {

    Object.assign(this.uploadData, this.data)
    if (this.res.length && this.limit == '1') {
      //   this.showRes = []
      let o = {}
      o[this.showKey] = this.res
      this.showRes.push(o)
    }
    if (!this.limit && this.res.length) {
      this.showRes = this.res
    }
    // console.log(this.res)
    // console.log(this.showRes)
    this.action = api_url + this[this.type]
  },
  methods: {
    removeImg(val) {
      this.showRes.map((e, i) => {
        if (e.file_path == val) {
          this.showRes.splice(i, 1)
        }
      })
      this.$emit('removeImg', val)
      //   this.clear()
    },
    before(file) {
      this.loading = true
      return true
    },
    success(res) {
      //   console.log(res)
      this.loading = false
      let tempObj = {}
      if (this.limit == '1') {
        this.showRes.splice(0, this.showRes.length, res.data)
        // console.log(this.res)
        // console.log(this.showRes)
      }
      //   if (res.code * 1 === 200 && this.limit != '1') {
      tempObj = {
        name: res.data.name,
        url: res.data.file_path,
        savepath: res.data.savepath,
        file_path: res.data.file_path
      }
      // Object.assign(this.showRes, res.data)
      //   }
      //   console.log(this.showRes)
      //   console.log(this.res)

      this.$emit('success', tempObj)
    },
    error(...res) {
      this.loading = false
      this.$emit('error', ...res)
    },
    clear() {
      this.$refs.uploadDom.clearFiles()
    },
    showPreview(file) {
    //   console.log(file)
      this.previewImg = file.url || file
      this.imgVis = true
      // console.log(file)
      this.$emit('showPreview', file)
    }
  }
}
</script>

<style scoped lang="less">
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  .dis {
    display: none;
  }
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
  .dis {
    display: block;
    background-color: rgba(0, 0, 0, 0.5);
    width: 178px;
    height: 178px;
    position: absolute;
    top: 0;
  }
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
  border: 1px dashed #ccc;
  border-radius: 4px;
}
.dis {
  // background-color: #000;
  // opacity: .1;
}
.el-icon-delete,
.el-icon-zoom-in {
  font-size: 22px;
  color: #fff;
}
.el-icon-zoom-in {
  position: absolute;
  left: 56px;
  top: 78px;
}
.el-icon-delete {
  position: absolute;
  left: 96px;
  top: 78px;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
//   .gl-update {
//     display: inline-block;
//     vertical-align: middle;
//     width: 150px;
//     height: 150px;
//     .img {
//       width: 100%;
//       max-height: 100%;
//     }
//     /deep/ .el-upload {
//       width: 100%;
//       height: 100%;
//     }
//   }

//   .add-icon {
//     position: relative;
//     display: block;
//     border: 1px #aaa solid;
//     box-sizing: border-box;
//     border-radius: 5px;
//     width: 100%;
//     height: 100%;
//     box-shadow: 0 0 5px #aaa inset;
//     text-shadow: 0 0 5px #aaa;
//     &::before {
//       position: absolute;
//       top: 50%;
//       left: 50%;
//       transform: translate(-50%, -50%);
//     }
//   }
</style>
